import Vue from "vue"
import VueRouter from "vue-router"
// import Index from '../view/index.vue'
Vue.use(VueRouter)
const Login = () => import(/* webpackChunkName: "login_index" */ '../view/login/login.vue')
const Index = () => import(/* webpackChunkName: "login_index" */ '../view/index.vue')

const Home = () => import(/* webpackChunkName: "home_publicArtic_previewArticle" */ '../view/home.vue')
const PublicArtic = () => import(/* webpackChunkName: "home_publicArtic_previewArticle" */ '../view/article/publicArtic.vue')
const PreviewArticle = () => import(/* webpackChunkName: "home_publicArtic_previewArticle" */ '../view/article/previewArticle.vue')

const TestVuex = () => import(/* webpackChunkName: "testVuex_scanLogin_cssgame" */ '../view/testVuex.vue')
const ScanLogin = () => import(/* webpackChunkName: "testVuex_scanLogin_cssgame" */ '../view/login/scanLogin.vue')
const Cssgame = () => import(/* webpackChunkName: "testVuex_scanLogin_cssgame" */ '../view/cssGame/cssgame.vue')

const routes = [
  { path: '/', component: Index },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
  },
  {
    path: '/publicArtic',
    component: PublicArtic,
  },
  {
    path: '/previewArticle',
    component: PreviewArticle,
  },
  {
    path: '/testVuex',
    component: TestVuex,
  },
  {
    path: '/scanLogin',
    component: ScanLogin,
  },
  {
    path: '/cssgame',
    component: Cssgame,
  }
]

// 写文章需要token,
const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
})
// 挂载路由导航守卫
let whitlist = ["/publicArtic"]
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path === "/login") return next()
  // 获取token
  const tokenStr = window.localStorage.getItem("token")
  if (whitlist.includes(to.path) && !tokenStr) {
    return next("/login")
  }
  next()
})

export default router
